//
//  SWHSystemInfoView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/7/25.
//

import UIKit
import SnapKit
import RxSwift
import RxCocoa
class SWHDetailSystemInfoView: CustomBackView {
    fileprivate lazy var disposeBag = DisposeBag()
    let topImageView = UIImageView()
    let stcsLabel = PaddingLabel()
    let editSystemInfoBtn = ContentLabel(R.font.interMedium(size: 15),hexStr: "#A9ABADFF")
    let tankTableView = UITableView()
    //contraint
    var tankTableViewHeightConstraint: Constraint?
    let tankMoreImageView = ViewFactory.shared.generateMoreImageView()
    
    override func setUpView() {
        //topImageView
        topImageView.drawCorner(byRoundingCorners: [.topLeft,.topRight])
        topImageView.backgroundColor = UIColor(hexStr: "#70798A")
        topImageView.image = R.image.swhdetail_systeminfo_top_icon()
        topImageView.contentMode = .scaleAspectFit
        self.addSubview(topImageView)
        topImageView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(80)
        }
        //stcsLabel
        stcsLabel.backgroundColor = UIColor(hexStr: "#F5F5F5FF")
        stcsLabel.layer.cornerRadius = 15
        stcsLabel.setFont(R.font.interMedium(size: 13))
        stcsLabel.clipsToBounds = true
        self.addSubview(stcsLabel)
        stcsLabel.snp.makeConstraints { make in
            make.top.equalTo(topImageView.snp.bottom).offset(16)
            make.left.equalTo(16)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //edit btn
        editSystemInfoBtn.text = "Edit"
        editSystemInfoBtn.textAlignment = .right
        self.addSubview(editSystemInfoBtn)
        editSystemInfoBtn.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.height.width.equalTo(40)
            make.centerY.equalTo(stcsLabel)
        }
        //panel tableview cell
        self.addSubview(tankTableView)
        tankTableView.snp.makeConstraints { make in
            make.top.equalTo(editSystemInfoBtn.snp.bottom)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            self.tankTableViewHeightConstraint = make.height.greaterThanOrEqualTo(0).constraint
            make.bottom.equalTo(-16)
        }
        //panel的more箭头
        self.addSubview(tankMoreImageView)
        tankMoreImageView.snp.makeConstraints { make in
            make.top.equalTo(tankTableView).offset(CREATEJOB_REVIEW_INTERNAL)
            make.right.equalTo(tankTableView)
            make.width.height.equalTo(24)
        }
        tankTableView.separatorStyle = .none
        tankTableView.register(SWHDetailTankTableViewCell.self, forCellReuseIdentifier: SWHDetailTankTableViewCell.reuseCellIdentifier)
        
        //监听属性变化
        tankTableView.rx.observe(CGSize.self,"contentSize").subscribe {[weak self] (size) in
            guard let contentSize = size else {return}
            self?.tankTableViewHeightConstraint?.update(offset: contentSize.height)
        }.disposed(by: self.disposeBag)
        
        //
        self.bindData()
    }
    private func bindData(){
        //stcs
        JobDetailModelHelper.shared.system_detail.map({$0.rec_quantity}).subscribe(onNext: { [weak self] (data) in
            guard let strongSelf = self else {return}
            strongSelf.stcsLabel.attributedText = strongSelf.getStcsAttributeString(data)
        }).disposed(by: self.disposeBag)
        //设置cell信息，tank绑定数据源
        JobDetailModelHelper.shared.system_detail.map({$0.heaters}).bind(to: tankTableView.rx.items(cellIdentifier: SWHDetailTankTableViewCell.reuseCellIdentifier,cellType: SWHDetailTankTableViewCell.self)){(row, model, cell) in
            cell.setData(model,firstRowFlag: (row == 0 ? true: false))
        }.disposed(by: self.disposeBag)
    }
    //MARK: - 是否显示panel，inverter ，battery tableview右上角的接头（显示表示可以编辑）,以及整个system的edit 按钮
    func checkIfShowMoreImageView(){
        self.editSystemInfoBtn.isHidden = !JobDetailModelHelper.shared.jobDetailModel.canEditJobDetailFlag
        let tanksCnt = JobDetailModelHelper.shared.jobDetailModel.system_detail.heaters.count
        if  JobDetailModelHelper.shared.jobDetailModel.canEditJobDetailFlag && tanksCnt > 0 {
            self.tankMoreImageView.isHidden = false
        }else{
            self.tankMoreImageView.isHidden = true
        }
    }
    private func getStcsAttributeString(_ stcs: String)->NSMutableAttributedString{
        let firstStr = "STCs "
        let secStr = stcs
        let attrString = NSMutableAttributedString(string: firstStr + secStr)
        let strSubAttr1: [NSMutableAttributedString.Key: Any] = [.foregroundColor: UIColor(hexStr: "#292D32FF")]
        let strSubAttr2: [NSMutableAttributedString.Key: Any] = [.foregroundColor: UIColor(hexStr: "#379657FF")]
        attrString.addAttributes(strSubAttr1, range: NSRange(location: 0, length: firstStr.count))
        attrString.addAttributes(strSubAttr2, range: NSRange(location: firstStr.count, length: secStr.count))
        return attrString
    }
}
